//
//  UIImage+Utilities.m
//  FiveHealth
//
//  Created by JingGuo on 2017/11/12.
//  Copyright © 2017年 com.wukangcheng.FiveHealth. All rights reserved.
//

#import "UIImage+Utilities.h"

@implementation UIImage (Utilities)

+ (UIImage *)imageFromColors:(NSArray *)colors
                   withFrame:(CGRect)frame
        andColorGradientType:(FHColorGradientType)type {
    
    NSMutableArray *ar = [NSMutableArray array];
    
    for (UIColor *c in colors) {
        
        [ar addObject:(id) c.CGColor];
        
    }
    
    UIGraphicsBeginImageContextWithOptions(frame.size, YES, 1);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSaveGState(context);
    
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef) ar, NULL);
    
    CGPoint start;
    
    CGPoint end;
    
    switch (type) {
        case FHGradientTypeLiftToRight:
        {
            start = CGPointMake(0.0, 0.0);
            
            end = CGPointMake(frame.size.width, 0.0);
        }
            break;
        case FHGradientTypeTopToDown:
        {
            start = CGPointMake(0.0, 0.0);
            
            end = CGPointMake(0.0, frame.size.height);
        }
            break;
        case FHGradientTypeDiagonal:
        {
            start = CGPointMake(0.0, 0.0);
            
            end = CGPointMake(frame.size.width, frame.size.height);
        }
            break;
            
        default:
            break;
    }
    
    
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    CGGradientRelease(gradient);
    
    CGContextRestoreGState(context);
    
    //    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    return [self imageWithColor:color size:CGSizeMake(1, 1)];
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)placeholderImageCircular {
    return [UIImage imageNamed:@"zhanweitu_yuan"];
}

+ (UIImage *)placeholderImageQuadrate {
    return [UIImage imageNamed:@"zhanweitu_fang"];
}


- (UIImage *)formateFixOrientation
{
    if (self.imageOrientation == UIImageOrientationUp)
        return self;

    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (self.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    switch (self.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                             CGImageGetBitsPerComponent(self.CGImage), 0,
                                             CGImageGetColorSpace(self.CGImage),
                                             CGImageGetBitmapInfo(self.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (self.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
            break;
    }
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
    
}
- (UIImage *)getFormateImgWithRatio:(float)whImgRatio
{
    CGFloat ratioSelf = self.size.width/self.size.height;
    CGFloat ratioPic =  whImgRatio;//宽高比
    
    float cutX = 0,
    cutY = 0,
    cutWid = self.size.width,
    cutHeight = self.size.height;
    if (ratioSelf >= ratioPic )
    {
        float tempImgWid = self.size.height*ratioPic;
        cutX = (tempImgWid-self.size.width)/2.0;
        cutWid = tempImgWid;
    }else{
        float tempImgH = cutWid/ratioPic;
        cutY = (self.size.height-tempImgH)/2.0;
        cutHeight = tempImgH;
    }
    CGRect rect = CGRectMake(cutX,cutY,cutWid,cutHeight);
    
    CGImageRef imageRef = self.CGImage;
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef,rect);
    UIImage * cropImage = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    
    return cropImage;
}
//截取图片中部位置3x2大小的图片区域
- (UIImage *)get3x2CutImg
{
    CGFloat ratioSelf = self.size.width/self.size.height;
    CGFloat ratioPic =  3/2.0f;//宽高比3比2
    
    float cutX = 0,
          cutY = 0,
        cutWid = self.size.width,
     cutHeight = self.size.height;
    if (ratioSelf >= ratioPic )
    {
        float tempImgWid = self.size.height*ratioPic;
        cutX = (tempImgWid-self.size.width)/2.0;
        cutWid = tempImgWid;
    }else{
        float tempImgH = cutWid/ratioPic;
        cutY = (self.size.height-tempImgH)/2.0;
        cutHeight = tempImgH;
    }
    CGRect rect = CGRectMake(cutX,cutY,cutWid,cutHeight);
    
    CGImageRef imageRef = self.CGImage;
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef,rect);
    UIImage * cropImage = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    
    return cropImage;
}

@end
